

var app = new Vue({
    el: '.box',
    data: {
        tableData: '',//表格数据
        dialogTableVisible: false,//显示弹框
        dialogTableVisible1: false,
        tabitem: '新建目录',//弹框标题
        gridData: '',//弹框数据
        formLabelWidth: '70px',
        pertab: '',//上级目录数据
        value: '',//所选目录
        type: "",//顶级菜单和下级菜单标识
        tab_value: '',//新建菜单所属上级
        tab_name: '',//新建菜单名称
        tab_flag: '',//新建菜单状态
        tab_href: '',//新建菜单地址
        per_type: false//设置顶级菜单或下级菜单
    },
    methods: {
        del_nav(obj) {
            var arr = {};
            arr.token = (sessionStorage.getItem('token'));
            if (this.tableData[obj].flag2) {
                arr.head = 1006;
                arr.ids = this.tableData[obj].Id;
                //顶级菜单
            } else {
                arr.head = 1007;
                arr.ids = this.tableData[obj].Id;
                //下级
            };

            this.$confirm('此操作将删除该目录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                del(arr);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        set_nav(obj) {
            this.tabitem = '目录详情';
            this.dialogTableVisible = true;
            this.gridData = this.tableData[obj];
            this.value = this.tableData[obj].indexs;
            if (this.gridData.flag2) {
                this.type = '1';
                //顶级菜单
            } else {
                this.type = '0';
                //下级菜单
            };
        },
        set_naves(falg) {
            if (this.per_type) {
                this.dialogTableVisible1 = false;
                var arr = {};
                arr.head = 1008;
                arr.token = (sessionStorage.getItem('token'));
                arr.indexs = (this.pertab.length + 1);
                arr.name = this.tab_name;
                arr.flag = this.tab_flag;
                arr.href = this.tab_href;
                save(arr);
                //新建顶级菜单
            } else {
                if (falg) {
                    this.dialogTableVisible = false;
                    var arr = this.gridData;
                    arr.indexs = this.value;
                    arr.head = 1004;
                    arr.type = this.type;
                    arr.ids = this.gridData.Id;
                    arr.token = (sessionStorage.getItem('token'));
                    save(arr);
                    //更新顶级菜单
                } else {
                    this.dialogTableVisible1 = false;
                    var arr = {};
                    arr.head = 1005;
                    arr.token = (sessionStorage.getItem('token'));
                    arr.indexs = this.tab_value;
                    arr.name = this.tab_name;
                    arr.flag = this.tab_flag;
                    arr.href = this.tab_href;
                    save(arr);
                    //更新下级菜单
                };
            };
        }
    }
});


function del(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",//类型
        baseURL: '../../api/nav.php',//地址
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '操作成功~',
                type: 'success'
            });
            get_nav();
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);//失败返回错误信息
    });//获取nav
};

function save(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",//类型
        baseURL: '../../api/nav.php',//地址
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '操作成功~',
                type: 'success'
            });
            get_nav();
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);//失败返回错误信息
    });//获取nav
};



get_nav();
function get_nav() {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    var arr = [];
    axios({
        method: "POST",//类型
        baseURL: '../../api/admin.php',//地址
        data: 'data=' + JSON.stringify({ head: 1009, token: (sessionStorage.getItem('token')) })
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.per_type = false;
            app.pertab = res.data.data;
            console.log(res.data.data);
            for (var i = 0; i < res.data.data.length; i++) {
                res.data.data[i].flag1 = res.data.data[i].flag === '1' ? true : false;//状态
                res.data.data[i].flag2 = true;//顶级标志位
                arr.push(res.data.data[i]);
                if (res.data.data[i].list) {
                    for (var j = 0; j < res.data.data[i].list.length; j++) {
                        res.data.data[i].list[j].flag1 = res.data.data[i].list[j].flag === '1' ? true : false;
                        res.data.data[i].list[j].flag2 = false;
                        arr.push(res.data.data[i].list[j]);
                        if (j === i) {
                            app.tableData = arr;
                            console.log(arr);
                        };
                    };
                };
            };
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);//失败返回错误信息
    });//获取nav
};





